package phoenics.manpads.common.mybatis.page;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import phoenics.manpads.common.mybatis.page.dialect.MysqlDialect;
import phoenics.manpads.common.mybatis.page.dialect.OracleDialect;

/**
 * page sql 解析
 *
 * @author Phoenics Chow
 * on 2018年12月09日
 */
public class PageSqlFactory {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PageSqlFactory.class);

    /**
     * 根据数据库方言获取分页查询语句（目前只支持mysql、oracle；默认mysql）
     * @param dialect
     * @param originalSql
     * @param rowBounds
     * @return
     */
    public static String getPageSqlByDialect(String dialect, String originalSql, RowBounds rowBounds) {
        String pageSql = "";
        if(StringUtils.isBlank(dialect)){
            return pageSql;
        }
        dialect=dialect.toLowerCase();
        switch (dialect){
            case "mysql" :
                pageSql=new MysqlDialect().buildPaginationSql(originalSql,rowBounds.getOffset(),rowBounds.getLimit());
                break;
            case "oracle" :
                pageSql=new OracleDialect().buildPaginationSql(originalSql,rowBounds.getOffset(),rowBounds.getLimit());
                break;
            default:
                throw new IllegalArgumentException(" dialect : " + dialect + "was not supported'");
        }

        return pageSql;
    }
}
